﻿using System;
using System.Data;
using System.Data.SqlServerCe;
using MWTools.Client.Common;
using System.Reflection;
using MWTools.Client.Common.Interfaces;

namespace MWTools.Client.OutlookHelper.Dal
{
    public class ClientSettingDao
    {
        #region ClientSettings CRUD

        public static DataTable ClientSettingSelect()
        {
            DataTable dataTable = new DataTable();

            using (SqlCeConnection connection = new SqlCeConnection(string.Format("Data Source={0}", Constants.OUTLOOK_STORE_PATH)))
            {
                string commandText = string.Format("SELECT * FROM UserSettings");

                using (SqlCeCommand command = new SqlCeCommand(commandText, connection))
                {
                    connection.Open();
                    using (SqlCeDataAdapter sda = new SqlCeDataAdapter(command))
                    {
                        sda.Fill(dataTable);
                    }
                }
            }


            return dataTable;
        }
        public static void ClientSettingInsert( string userName, string passwordHash)
        {
            DataTable dt = new DataTable();

            using (SqlCeConnection connection = new SqlCeConnection(string.Format("Data Source={0}", Constants.OUTLOOK_STORE_PATH)))
            {
                string commandText = string.Format("INSERT INTO UserSettings ( EMailAddress, Password) VALUES ('{0}', '{1}');",
                                         userName,
                                         passwordHash);


                using (SqlCeCommand command = new SqlCeCommand(commandText, connection))
                {
                    SqlCeDataAdapter sda = new SqlCeDataAdapter(command);
                    try
                    {
                        sda.Fill(dt);
                    }
                    catch (Exception ex)
                    {
                        Logger.FatalFormat("ClientSettingsDao.ClientSettingsInsert","{0}", ex);
                        throw;                   
                    }
                }
            }
        }
        public static void ClientSettingDelete(string email)
        {
            using (SqlCeConnection connection = new SqlCeConnection(string.Format("Data Source={0}", Constants.OUTLOOK_STORE_PATH)))
            {
                string commandText = string.Format("DELETE FROM UserSettings where EmailAddress = '{0}'", email);

                using (SqlCeCommand command = new SqlCeCommand(commandText, connection))
                {
                    connection.Open();
                    Logger.DebugFormat(MethodBase.GetCurrentMethod().DeclaringType, "executing sql{0}", commandText);
                    try
                    {
                        command.CommandType = CommandType.Text;
                        command.ExecuteNonQuery();          
                    }
                    catch (Exception ex)
                    {
                        Logger.FatalFormat("ClientSettingsDao.ClientSettingsDelete","{0}", ex);
                        
                      throw;
                    }

                }
            }
     
        }
        #endregion
    }
}